<?php
/**
 * New user sign up form.
 * 
 * @package disputeo
 * @version 1.0
 * @author jimmy.dixon
 */

class Application_Form_Register extends Application_Form_Entry
{
    /**
     * @var string 
     */
    protected $_legend = 'Create a disputeo account.';
    
    /**
     * @var string 
     */
    protected $_usernameNotAlnum = 'User Name must contain alphanumeric characters only.';
    
    /**
     * @var string 
     */
    protected $_usernameTaken = 'Sorry, the user name "%value%" is already in use.';
    
    /**
     * @var string 
     */
    protected $_emailTaken = 'An account already exists using the email address "%value%".';
    
    /**
     * @var string 
     */
    protected $_emailInvalid = 'Sorry, "%value%" is not a valid email address.';
    
    /**
     * @var string 
     */
    protected $_emailEmpty = 'Please provide an email address.';
    
    /**
     * @var string 
     */
    protected $_emailTooLong = 'The email address you have provided is too long.';
    
    /**
     * @var string 
     */
    protected $_passwordNotIdentical = 'The two passwords entered must be indentical.';
    
    /**
     * Add form elements
     */
    public function init()
    {
        $this->setName('register');
        $this->setAction('/login/register');
        $this->_addEmailElement();
        $this->_addUserNameElement();
        $this->_addPasswordElement();
        $this->_addReTypePasswordElement();
        $this->addCaptcha();
        $this->_setDisplayGroup();
        $this->_addDisplayGroup('registerGroup', $this->_legend);
        $this->_addSubmitElement('Create Account');
        $this->_addValidators();
    }

    /**
     * Add email element to form
     */
    protected function _addEmailElement()
    {
        $email = new Zend_Form_Element_Text('email');
        $email->setLabel('Email Address')
            ->setOrder(0)
            ->setRequired(true)
            ->removeDecorator('Errors');
        $this->addElement($email);
    }

    /**
     * Add password2 element to form
     */
    protected function _addReTypePasswordElement()
    {
        $password2 = new Zend_Form_Element_Password('password2');
        $password2->setLabel('Re-type Password')
            ->setRequired(true)
            ->removeDecorator('Errors')
            ->setAttrib('renderPassword', true);
        $this->addElement($password2);
    }

    /**
     * Setup form element validation
     */
    protected function _addValidators()
    {
        $this->_addUsernameValidators();
        $this->_addEmailValidators();
        $this->_addPasswordValidators();
    }

    /**
     * Setup username element validation
     */
    protected function _addUsernameValidators()
    {
        $dbcheck = new Zend_Validate_Db_NoRecordExists(
                array('table' => 'users', 'field' => 'username'));
        $dbcheck->setMessage($this->_usernameTaken, Zend_Validate_Db_NoRecordExists::ERROR_RECORD_FOUND);
        $alnum = new Zend_Validate_Alnum();
        $alnum->setMessage($this->_usernameNotAlnum, Zend_Validate_Alnum::NOT_ALNUM);
        $this->getElement('username')
            ->addValidators(array($alnum, $dbcheck));
    }
    
    /**
     * Setup email element validation
     */
    protected function _addEmailValidators()
    {
        $emailErrors = array(Zend_Validate_EmailAddress::INVALID,
            Zend_Validate_EmailAddress::INVALID_FORMAT, Zend_Validate_EmailAddress::INVALID_HOSTNAME,
            Zend_Validate_EmailAddress::DOT_ATOM, Zend_Validate_EmailAddress::INVALID_LOCAL_PART,
            Zend_Validate_EmailAddress::INVALID_MX_RECORD, Zend_Validate_EmailAddress::INVALID_SEGMENT,
            Zend_Validate_EmailAddress::LENGTH_EXCEEDED
        );
        $emailValid = new Zend_Validate_EmailAddress();
        foreach ($emailErrors as $error) {
            $emailValid->setMessage($this->_emailInvalid, $error);
        }
        
        $dbcheck = new Zend_Validate_Db_NoRecordExists(
                array('table' => 'users', 'field' => 'email'));
        $dbcheck->setMessage($this->_emailTaken, Zend_Validate_Db_NoRecordExists::ERROR_RECORD_FOUND);
        $notEmpty = new Zend_Validate_NotEmpty();
        $notEmpty->setMessage($this->_emailEmpty, Zend_Validate_NotEmpty::IS_EMPTY);
        $length = new Zend_Validate_StringLength();
        $length->setMax(32)
            ->setMessage($this->_emailTooLong, Zend_Validate_StringLength::TOO_LONG);

        $this->getElement('email')
            ->addValidators(array($notEmpty, $length, $emailValid, $dbcheck));
    }
    
    /**
     * Setup password element validation
     */
    protected function _addPasswordValidators()
    {
        $identical = new Zend_Validate_Identical();
        $identical->setToken('password')
            ->setMessage($this->_passwordNotIdentical, Zend_Validate_Identical::NOT_SAME);
        $passwordEmpty = new Zend_Validate_NotEmpty();
        $passwordEmpty->setMessage($this->_passwordNotIdentical, Zend_Validate_NotEmpty::IS_EMPTY);
        $this->getElement('password2')
            ->addValidators(array($passwordEmpty, $identical));
    }

    /**
     * Set fieldset arrangement
     */
    protected function _setDisplayGroup()
    {
        $this->_displayGroup = array(
            'username', 'password', 'email', 'password2', 'captcha'
        );
    }

}

?>